import type {CEType, Viewer} from "vendor/components/pro-cesium";

/**
 * 添加3d tile建筑白模
 */
const addBuildWhiteMold = (CE: CEType, viewer: Viewer) => {
    const tileSetLayer = new CE.TilesetLayer("tileSetLayer");
    viewer.addLayer(tileSetLayer);

    let style = new CE.TilesetStyle()

    style.color = {
        // @ts-ignore
        conditions: [
            ['${Height} >= 300', 'rgba(45, 0, 75, 0.5)'],
            ['${Height} >= 200', 'rgb(102, 71, 151)'],
            ['${Height} >= 100', 'rgb(170, 162, 204)'],
            ['${Height} >= 50', 'rgb(224, 226, 238)'],
            ['${Height} >= 25', 'rgb(252, 230, 200)'],
            ['${Height} >= 10', 'rgb(248, 176, 87)'],
            ['${Height} >= 5', 'rgb(198, 106, 11)'],
            ['true', 'rgb(127, 59, 8)']
        ]
    }

    let tileset = new CE.Tileset('https://resource.dvgis.cn/data/3dtiles/ljz/tileset.json').setStyle(style)
    tileSetLayer.addOverlay(tileset);

    return tileset;
}

/**
 * 渲染雷达和扫描圈扫描
 */
export const renderRadarAndCircleScan = (CE: CEType, viewer: Viewer) => {
    addBuildWhiteMold(CE, viewer);

    viewer.setOptions({
        globe: {
            depthTestAgainstTerrain: true // 当前示例中的Shader渲染需要开启深度检测，并在无地形时效果好些。
        }
    });

    // 雷达扫描
    const radarScan =new CE.RadarScan( viewer,'121.489206,31.241320',1000,{
        speed: 3
    });

    // 扫描圈
    const circleScan = new CE.CircleScan(viewer,'121.489206,31.241320', 1000,{
        speed: 3
    });

    viewer.flyToPosition(new CE.Position(121.491415, 31.208443, 1954.04, 0, -28), () => {
        circleScan.start()
        radarScan.start()
    }, 3)
}

/**
 * 开场动画
 */
export const openingAnimation = (CE: CEType, viewer: Viewer) => {
    const globeRotate = new CE.GlobeRotate(viewer,
        {
            duration:3,
            callback: () => {
                viewer.flyToPosition(
                    new CE.Position(120.38105869, 31.10115627, 1e5, 0, -90),
                    () => {

                    }
                )
            }
        })

    globeRotate.start()
}

/**
 * 地球自旋转
 */
export const globeRotate = (CE: CEType, viewer: Viewer) => {
    const globeRotate = new CE.GlobeRotate(viewer)

    globeRotate.start()
}

/**
 * 定点巡航
 */
export const doFlying = (CE: CEType, viewer: Viewer) => {
    addBuildWhiteMold(CE, viewer);

    const flying = new CE.Flying(viewer,{
        loop: true,
        dwellTime:3,
    });

    flying.positions =  [
        {lng: 121.46748793889597, lat: 31.22345700031846, alt: 1082.6691622203975, heading: 0.9161118327237789, pitch: -38.63414039808751},
        {lng: 121.49543157056694, lat: 31.219611353179484, alt: 663.5376240776116, heading: 0.9161124649627334, pitch: -38.63418986635751},
        {lng: 121.53162234574106, lat: 31.228003869427294, alt: 1891.926162456467, heading: 298.6565902579582, pitch: -33.67285705092492},
        {lng: 121.54438164431083, lat: 31.25201585389836,  alt: 1441.4625182144541, heading: 298.65660919687264, pitch: -33.6728415156399}
    ];

    flying.start()
}

/**
 * 绕地环绕
 */
export const doAroundView = (CE: CEType, viewer: Viewer) => {
    viewer.flyToPosition(
        new CE.Position(120.1223, 31.1232, 1e5, 0, -90),
        () => {
            const aroundView = new CE.AroundView(viewer)
            aroundView.start()
        }
    )
}


/**
 * 绕点环绕
 */
export const doAroundPoint = (CE: CEType, viewer: Viewer) => {
    const tileset = addBuildWhiteMold(CE, viewer);
    const aroundPoint  = new CE.AroundPoint(viewer,'121.48914778697578,31.21498411014459',{pitch:-41,range:2000});
    viewer.flyTo(tileset)
    aroundPoint.start();
}